package com.example.demo.controller;

import com.example.demo.pojo.Customer;
import com.example.demo.service.CustomerService;
import com.example.demo.util.JwtTokenUtils;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "用户管理系统")
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    public CustomerService customerService;
    //0
    @PostMapping("/login")
    @ApiOperation("用户登入")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名",required = true),
            @ApiImplicitParam(name = "password",value = "密码",required = true)
    })
    public String login(String username,String password){//验证用户登入
        Customer customer=customerService.select(username, password);
        if (customer!=null){
            String token= JwtTokenUtils.createToken(username);
            return token;
        }
        return "登入失败";
    }
    //1
    @GetMapping("/select")
    @ApiOperation("根据ID进行查询")
    @ApiImplicitParam(name = "id",value = "用户编号",required = true)
    public Customer select(int id){
        Customer customer=customerService.select(id);
        return customer;
    }
    //2
    @PostMapping("/insert")
    @ApiOperation("用户新增")
    public String save(Customer customer){
        customerService.insert(customer);
        return "用户新增成功";
    }
    //3
    @PostMapping("/delete")
    @ApiOperation("用户注销")
    @ApiImplicitParam(name = "id",value = "用户编号",required = true)
    public String delete(int id){
        customerService.delete(id);
        return "用户注销成功";
    }
    //4
    @PostMapping("/update")
    @ApiOperation("用户更新")
    public String update(Customer customer ){
        customerService.update(customer);
        return "用户更新成功";
    }
    //5
    @PostMapping("/patchdelete")
    @ApiOperation("用户批量注销")
    @ApiImplicitParam(name = "ids",value = "用户编号，用英文逗号分开",required = true)
    public String patchdelete(int[] ids){
        for (int id: ids){customerService.delete(id );}
        return "用户批量注销成功";
    }
    //6
    @GetMapping("/selectByPage")
    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name="size",value = "页面大小",required = true),
            @ApiImplicitParam(name = "page",value = "页码",required = true)
    })
    public PageInfo seleceByPage(int size, int page){
        PageInfo pageInfo=customerService.findByPage(size,page);
        return pageInfo;
    }
}
